package tdh.xt.common.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @package com.xt.util
 * @description cookie相关的工具类
 * @author Ricardo
 * @date 2021/5/10 2:38
 * @copyright (c) 2021,南京通达海科技股份有限公司 All Rights Reserved.
 * @version 1.0
 * Modification History:
 * Date                  Author      Version     Description
 * ------------------------------------------------------------------
 * 2021/5/10 2:38       Ricardo       1.0     cookie相关的工具类
 */
public class CookieUtil {

    /**
     * @description 私有化构造方法，防止误初始化工具类
     * @author RicarAN
     * @date 2021/5/13 13:40
     * @version 1.0
     */
    private CookieUtil(){}

    /**
     * @param request    request
     * @param cookieName cookieName
     * @return boolean
     * @description 是否存在一个cookie
     * @author RicarAN
     * @date 2021/5/10 2:38
     * @version 1.0
     */
    public static boolean hasCookie(HttpServletRequest request, String cookieName) {
        Cookie[] cookies = request.getCookies();
        if (cookies == null){
            return false;
        }
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param request    request
     * @param cookieName cookie名
     * @return java.lang.String cookie值
     * @description 根据名字获取一个cookie的值
     * @author RicarAN
     * @date 2021/5/10 2:39
     * @version 1.0
     */
    public static String getValue(HttpServletRequest request, String cookieName) {
        if (!hasCookie(request, cookieName)) {
            return null;
        }
        Cookie[] cookies = request.getCookies();
        String res = "";
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals(cookieName)) {
                res = cookie.getValue();
            }
        }
        return res;
    }

    /**
     * @param response   response
     * @param cookieList cookieList
     * @param maxAge     存活时间
     * @description 将一个cookie集合设置到response中
     * @author RicarAN
     * @date 2021/5/10 2:40
     * @version 1.0
     */
    public static void setCookies(HttpServletResponse response, List<Cookie> cookieList, int maxAge) {
        for (Cookie cookie : cookieList) {
            cookie.setPath("/");
            cookie.setMaxAge(maxAge);
            response.addCookie(cookie);
        }
    }

    /**
     * @param request  request
     * @param response response
     * @description 移除所有的cookie
     * @author RicarAN
     * @date 2021/5/10 2:40
     * @version 1.0
     */
    public static void removeAllLoginCookies(HttpServletRequest request, HttpServletResponse response) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("token".equals(name) || "rememberYhid".equals(name) || "rememberYhkl".equals(name)) {
                removeCookie(response, cookie);
            }
        }
    }

    /**
     * @param response response
     * @description 移除单个cookie
     * @author RicarAN
     * @date 2021/5/10 2:40
     * @version 1.0
     */
    public static void removeCookie(HttpServletResponse response, Cookie cookie) {
        Cookie deleteCookie = new Cookie(cookie.getName(), null);
        deleteCookie.setMaxAge(0);
        deleteCookie.setPath("/");
        response.addCookie(deleteCookie);
    }
}
